package com.central.common.utils

import cn.hutool.http.HttpUtil
import java.text.DecimalFormat


//坐标转换api
const val coordinate_convert = "https://restapi.amap.com/v3/assistant/coordinate/convert"

//根据经纬度获取地址api
const val geocode_regeo = "https://restapi.amap.com/v3/geocode/regeo"

//获取两个经纬度之间的距离api
const val distance_get = "https://restapi.amap.com/v3/distance"

//高德应用key
const val map_key = "99c9ffe72056500963c1b551cfb134e2"

data class AddressInfo(var formattedAddress: String? = null, var citycode: String? = null)

fun coordinateConvert(point2D: Point2D): Point2D {

    HttpUtil.get(coordinate_convert, mapOf("key" to map_key, "locations" to point2D.longitude.toString() + "," + point2D.latitude.toString(),
        "coordsys" to "gps")).toJsonObject().run {

        return when (getInteger("status")) {
            0 -> {
                throw Exception("坐标转换失败${getString("info")}")
            }
            else -> {
                getString("locations").split(",").takeIf { it.isNotEmpty() }
                    ?.run { Point2D(this[0].toDouble(), this[1].toDouble()) }
                    ?: Point2D()
            }
        }
    }
}


fun geocodeRegeo(point2D: Point2D): AddressInfo {

    HttpUtil.get(geocode_regeo, mapOf("key" to map_key, "location" to coordinateConvert(point2D)
        .run { longitude.toString() + "," + latitude.toString() })).toJsonObject().run {

       return when (getInteger("status")) {
            0 -> {
                throw Exception("根据经纬度获取位置失败${getString("info")}")
            }
            else -> {
                getJSONObject("regeocode").getJSONObject("addressComponent").run {
                    AddressInfo(
                        formattedAddress = arrayListOf(getString("province"), getString("city"), getString("district"))
                            .filter { it != null && it != "" && it != "[]" }
                            .joinToString("-"),
                        citycode = getString("citycode"),
                    )
                }

            }
        }
    }
}


fun distanceGet(pointA: Point2D, pointB: Point2D): String {

    HttpUtil.get(distance_get, mapOf("key" to map_key,
        "origins" to coordinateConvert(pointA)
        .run { longitude.toString() + "," + latitude.toString() },
        "destination" to coordinateConvert(pointB)
            .run { longitude.toString() + "," + latitude.toString() })).toJsonObject().run {

        return when (getInteger("status")) {
            0 -> {
                throw Exception("根据经纬度获取距离${getString("info")}")
            }
            else -> {
                getJSONArray("results").getString(0).toJsonObject().getString("distance").toDouble()
                    .run {
                        if (this/1000 < 1) DecimalFormat("#.0").format(this)+"米"
                        else DecimalFormat("#.0").format(this/1000)+"公里"
                    }
            }
        }
    }
}



suspend fun main() {

    println(distanceGet(Point2D(116.397128, 39.916527), Point2D(116.430086, 39.982851)))
}
